package cn.edu.scau.cmi.tancuier.client;

import java.util.Set;

import cn.edu.scau.cmi.tancuier.hibernate.dao.StudentDAO;
import cn.edu.scau.cmi.tancuier.hibernate.dao.TeacherDAO;
import cn.edu.scau.cmi.tancuier.hibernate.domain.Student;
import cn.edu.scau.cmi.tancuier.hibernate.domain.Teacher;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

public class HibernateClient extends Application{

	public static void main(String[] args) {
		Application.launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		StudentDAO studentDAO = new StudentDAO();
		TeacherDAO teacherDAO = new TeacherDAO();
		primaryStage.setTitle("Hibernate");
		
		FlowPane flowPane = new FlowPane();
		flowPane.setPadding(new Insets(10, 20, 10, 20));
		flowPane.setHgap(5);
		flowPane.setVgap(5);
		
		//Student
		Label s = new Label("Student:");
		s.setFont(Font.font("Timer New Roman",FontWeight.BOLD, FontPosture.ITALIC, 20));
		s.setPrefWidth(250);
		flowPane.getChildren().add(s);
		
		Label s_id = new Label("ID: ");
		s_id.setPrefWidth(40);
		flowPane.getChildren().add(s_id);
		
		TextField s_idBox = new TextField();
		flowPane.getChildren().add(s_idBox);
		
		Label s_name = new Label("Name: ");
		s_name.setPrefWidth(40);
		flowPane.getChildren().add(s_name);
		
		TextField s_nameBox = new TextField();
		flowPane.getChildren().add(s_nameBox);
		
		Button s_add = new Button("Add");
		flowPane.getChildren().add(s_add);
		s_add.setOnAction(e->{
			Long id = Long.parseLong(s_idBox.getText());
			String name = s_nameBox.getText();
			Student student = new Student();
			student.setId(id);
			student.setName(name);
			studentDAO.save(student);
			System.out.print("Saved."); //debug
		});
		
		Button s_delete = new Button("Delete");
		flowPane.getChildren().add(s_delete);
		s_delete.setOnAction(e->{
			Long id = Long.parseLong(s_idBox.getText());
			Student student = studentDAO.getById(id);
			if(student != null) {
				studentDAO.delete(student);
				System.out.print("Deleted."); //debug
			}
			else {
				System.out.print("Failed."); //debug
			}
		});
		
		Button s_modify = new Button("Modify");
		flowPane.getChildren().add(s_modify);
		s_modify.setOnAction(e->{
			Long id = Long.parseLong(s_idBox.getText());
			String name = s_nameBox.getText();
			Student student = studentDAO.getById(id);
			if(student != null) {
				student.setName(name);
				studentDAO.update(student);
				System.out.print("Modified."); //debug
			}
			else {
				System.out.print("Failed."); //debug
			}
		});
		
		Label tutor_id = new Label("Tutor_id: ");
		flowPane.getChildren().add(tutor_id);
		
		TextField tutorBox = new TextField();
		flowPane.getChildren().add(tutorBox);
		
		Button tutor_add = new Button("Add Tutor");
		flowPane.getChildren().add(tutor_add);
		tutor_add.setOnAction(e->{
			Long studentID = Long.parseLong(s_idBox.getText());
			Long teacherID = Long.parseLong(tutorBox.getText());
			
			Student student = studentDAO.getById(studentID);
			Teacher teacher = teacherDAO.getById(teacherID);
			if(student != null && teacher != null) {
				Set<Student> students = teacher.getStudents();
				students.add(student);
				teacher.setStudents(students);
				
				student.setTeacher(teacher);
				studentDAO.attachDirty(student);
				System.out.print("Add tutor succeeded!");
			}
			else {
				System.out.print("Failed.");
			}
		});
		
		Button tutor_modify = new Button("Change Tutor");
		flowPane.getChildren().add(tutor_modify);
		tutor_modify.setOnAction(e->{
			Long studentID = Long.parseLong(s_idBox.getText());
			Long teacherID = Long.parseLong(tutorBox.getText());
			
			Student student = studentDAO.getById(studentID);
			Teacher teacher = teacherDAO.getById(teacherID);
			if(student != null && teacher != null) {
				student.setTeacher(teacher);
				studentDAO.attachDirty(student);
				System.out.print("Change tutor succeeded!");
			}
			else{
				System.out.print("Failed.");
			}
		});
		
		Label space = new Label("");
		space.setPrefWidth(200);
		flowPane.getChildren().add(space);
		
		//Teacher
		Label t = new Label("Teacher:");
		t.setFont(Font.font("Timer New Roman",FontWeight.BOLD, FontPosture.ITALIC, 20));
		t.setPrefWidth(250);
		flowPane.getChildren().add(t);
		
		Label t_id = new Label("ID: ");
		t_id.setPrefWidth(40);
		flowPane.getChildren().add(t_id);
		
		TextField t_idBox = new TextField();
		flowPane.getChildren().add(t_idBox);
		
		Label t_name = new Label("Name: ");
		t_name.setPrefWidth(40);
		flowPane.getChildren().add(t_name);
		
		TextField t_nameBox = new TextField();
		flowPane.getChildren().add(t_nameBox);
		
		Button t_add = new Button("Add");
		flowPane.getChildren().add(t_add);
		t_add.setOnAction(e->{
			Long id = Long.parseLong(t_idBox.getText());
			String name = t_nameBox.getText();
			Teacher teacher = new Teacher();
			teacher.setId(id);
			teacher.setName(name);
			teacherDAO.save(teacher);
			System.out.print("Saved."); //debug
		});
		
		Button t_delete = new Button("Delete");
		flowPane.getChildren().add(t_delete);
		t_delete.setOnAction(e->{
			Long id = Long.parseLong(t_idBox.getText());
			Teacher teacher = teacherDAO.getById(id);
			if(teacher != null) {
				teacherDAO.delete(teacher);
				System.out.print("Deleted."); //debug
			}
			else {
				System.out.print("Failed."); //debug
			}
		});
		
		Button t_modify = new Button("Modify");
		flowPane.getChildren().add(t_modify);
		t_modify.setOnAction(e->{
			Long id = Long.parseLong(t_idBox.getText());
			String name = t_nameBox.getText();
			Teacher teacher = teacherDAO.getById(id);
			if(teacher != null) {
				teacher.setName(name);
				teacherDAO.update(teacher);
				System.out.print("Modified."); //debug
			}
			else {
				System.out.print("Failed."); //debug
			}
		});
		
		Label student_id = new Label("Student_id: ");
		flowPane.getChildren().add(student_id);
		
		TextField studentBox = new TextField();
		flowPane.getChildren().add(studentBox);
		
		Button student_add = new Button("Add Student");
		flowPane.getChildren().add(student_add);
		student_add.setOnAction(e->{
			Long studentID = Long.parseLong(studentBox.getText());
			Long teacherID = Long.parseLong(t_idBox.getText());
			
			Student student = studentDAO.getById(studentID);
			Teacher teacher = teacherDAO.getById(teacherID);
			if(student != null && teacher != null) {
				Set<Student> students = teacher.getStudents();
				students.add(student);
				teacher.setStudents(students);
				
				student.setTeacher(teacher);
				studentDAO.attachDirty(student);
				System.out.print("Add student succeeded!");
			}
			else {
				System.out.print("Failed.");
			}
		});
		
		Button student_modify = new Button("Delete Student");
		flowPane.getChildren().add(student_modify);
		student_modify.setOnAction(e->{
			Long studentID = Long.parseLong(studentBox.getText());
			Long teacherID = Long.parseLong(t_idBox.getText());
			
			Student student = studentDAO.getById(studentID);
			Teacher teacher = teacherDAO.getById(teacherID);
			Teacher oldTeacher = student.getTeacher();
			if(student != null && teacher != null && teacher.getId() == oldTeacher.getId()) {
				Set<Student> students = teacher.getStudents();
				students.remove(student);
				teacher.setStudents(students);
				
				student.setTeacher(null);
				studentDAO.attachDirty(student);
				System.out.print("Delete student succeeded!");
			}
			else {
				System.out.print("Failed.");
			}
		});
		
		Scene scene = new Scene(flowPane, 250, 500);
		primaryStage.setScene(scene);
		primaryStage.show();
	}

}
